<?php
	include 'php/config.php';
	session_start();
	if (!isset($_SESSION["id_utente"])) {
		header("Location: " . $ISSUER_HOST . "/login.php");
		exit();
	} else if (!$_SESSION["admin"]) {
		header("Location: " . $ISSUER_HOST . "/forbidden.php");
		exit();
	}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf8" />
<title>Open Badges</title>
<link href="style.css" rel="stylesheet" type="text/css" />
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.2/themes/cupertino/jquery-ui.css" />
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.2/jquery-ui.js"></script>
<script src="js/utils.js"></script>
<script src="js/issue.js"></script>
<script>
$(function() {
	$( "#expires" ).datepicker({ minDate: 0, dateFormat: "yy-mm-dd" });
	$('input[type="button"]').button();
});
</script>
</head>
<body>
<div id="topPan"><img src="images/logo.gif" alt="Education Zone" width="245" height="37" border="0"  class="logo" title="Education Zone"/>
  <p>Open Badges</p>
  <div id="topContactPan"> </div>
  <div id="topMenuPan">
    <div id="topMenuLeftPan"></div>
    <div id="topMenuMiddlePan">
      <ul>
        <li><a href="index.php">Home</a></li>
        <li><a href="badges_page.php">Badges</a></li>
        <li><a href="displayer_page.php">Displayer</a></li>
        <li><a href="universities.php">Università</a></li>
        <li><a href="refs.php">Riferimenti</a></li>
        <li><a href="contacts.php">Contatti</a></li>
        <?php
		if (!$_SESSION['id_utente']) {
    	?>
        <li class="contact"><a id="menuLogin" class="contact" href="login.php">Login</a></li>
	<?php
		} else {
	?>
	<li class="contact"><a class="contact" href="logout.php">Logout</a></li>
	<?php
		}
	?>
      </ul>
    </div>
    <div id="topMenuRightPan"></div>
  </div>
</div>
<div id="bodyPan">
  <div id="bodyLeftPan">
    <h2><span>Open Badges - Issue</span></h2>
    <p id="message" class="errorMsg"></p>
    <p><span>Da questa pagina è possibile assegnare una badge</span></p>
	<?php
		// Connecting, selecting database
		$mysqli = new mysqli($MYSQL_HOST, $MYSQL_USERNAME, $MYSQL_PASSWORD, $MYSQL_DB_NAME);
		if ($mysqli->connect_errno) {
			echo "<p>Failed to connect to MySQL: " . $mysqli->connect_error . "</p>";
		} else {
			$mysqli->set_charset("utf8");
			$queries_ok = true;
			$badge_query = "SELECT `badgeId`, `nome`, `condivisa` FROM `badge`";
			$badges = $mysqli->query($badge_query);
			if (!$badges) {
				echo "<p>Failed to run query " . $badge_query . ": (" . $mysqli->errno . ") " . 
					$mysqli->error . "</p>";
				$queries_ok = false;
			}
			
			$user_query = "SELECT `userId`, `nome`, `cognome`, `email` FROM `user` WHERE `admin`=FALSE";
			$users = $mysqli->query($user_query);
			if (!$users) {
				echo "<p>Failed to run query " . $user_query . ": (" . $mysqli->errno . ") " .
						$mysqli->error . "</p>";
				$queries_ok = false;
			}
			
			if ($queries_ok && $badges->num_rows && $users->num_rows) {
		
	?>
	<form action="php/scripts/create_assertion.php" method="post">
	<table>
	<tr>
	<td>Badge</td>
	<td><select id="badge" name="badge" onchange="validateIssueForm()">
		<option value="">Scegli una badge</option>
	<?php
		while ($row = $badges->fetch_assoc()) {
    		echo "<option value=\"" . $row['badgeId'] . "#" . $row['nome'] . "#" . $row['condivisa'] . "\">" . $row['nome'] . "</option>";    
		}
	?>
	</select></td>
	</tr>
	<tr>
	<td>Earner</td>
	<td><select id="user" name="user" onchange="validateIssueForm()">
		<option value="">Scegli un utente</option>
	<?php
		while ($row = $users->fetch_assoc()) {
    		echo "<option value=" . $row['userId'] . "#" . $row['email'] . ">" . 
    			$row['nome'] . " " . $row['cognome'] . " (" . $row['email'] . ")</option>";    
		}
	?>
	</select></td>
	<tr>
	<td>Tipo<br>Asserzione</td>
	<td>
		<input type="radio" name="type" value="hosted" checked="checked" > Hosted<br>
  		<input type="radio" name="type" value="signed" disabled="disabled"> Signed<br></td>
	</tr>
	<tr>
	<td>Evidence</td>
	<td><input id="evidenceUrl" name="evidenceUrl" type="text" maxLength="100" /></td>
	</tr>
	<tr>
	<td>Expires</td>
	<td><input id="expires" name="expires" type="text" /></td>
	</tr>
	</table>
	<div id="message"></div>
	<input id="issue" type="submit" value="Assegna" disabled="disabled" />
	</form>
	<?php
			} else { // end if (queries_ok && ... )
				if (!$badges->num_rows) {
					echo "<p>Non ci sono badges da assegnare</p>";
					echo "<p>Crea una badge: <a href=\"new_badge_page.php\">Crea</a></p>";
				}
				
				if (!$users->num_rows) {
					echo "<p>Non ci sono utenti a cui assegnare badges</p>";
					echo "<p>Crea un nuovo utente: <a href=\"new_user_page.php\">Utente</a></p>";
				}
			}
			mysqli_free_result($users);		
			mysqli_free_result($badges);
			$mysqli->close();
		} // end else (if connection to db was ok) 
	?>
  </div>
  <div id="bodyRightPan">
    <?php
	if (!$_SESSION['id_utente']) {
    ?>
    <h2 id="login"><span>Log</span>in</h2>
    <form id="loginForm">
    <p class="boldtext">Username</p>
    <p><input id="username" name="username" type="text"/></p>
    <p id="userMsg" class="errorMsg"></p>
    <p class="boldtext">Password</p>
    <p><input id="password" name="password" type="password"/></p>
    <p id="pswMsg" class="errorMsg"></p>
    <p><input type="button" value="Login" onclick="validateLoginForm()"/></p>
    </form>
    <?php
	}
    ?>
    <h2><span>Quick</span> links</h2>
    <ul>
      <li><a href="index.php">Home</a> </li>
      <?php
	if ($_SESSION['id_utente']) {
		if ($_SESSION['admin']) {
      ?>
      <li><a href="issue_page.php">Assegna badge</a> </li>
      <li><a href="new_badge_page.php">Nuova badge</a> </li>
      <li><a href="categories_page.php">Gestisci categorie</a> </li>
      <li><a href="new_user_page.php">Nuovo utente</a> </li>
      <?php
		} else {
      ?>
      <li><a href="personal_page.php">Pagina personale</a> </li>
      <?php
		}
      ?> 
      <li><a href="badges_page.php">Badges</a> </li>
      <li><a href="displayer_page.php">Displayer</a> </li>     
      <li><a href="change_password_page.php">Cambia password</a> </li>
      <li><a href="logout.php">Logout</a> </li>
      <?php
		} else {
      ?>
      <li><a href="badges_page.php">Badges</a> </li>
      <li><a href="displayer_page.php">Displayer</a> </li>
      <?php
		}
      ?> 
    </ul>
  </div>
</div>
	<div id="footermainPan">
	  <div id="footerPan">
	    <ul>
	      <li><a href="index.php">Home</a>| </li>
	      <li><a href="badges_page.php">Badges</a>| </li>
	      <li><a href="displayer_page.php">Displayer</a>| </li>
      	      <?php if ($_SESSION['id_utente'] && !$_SESSION['admin']) { ?><li><a href="personal_page.php">Pagina personale</a>| </li> <?php } ?>
	      <li><a href="universities.php">Università</a>| </li>
	      <li><a href="refs.php">Riferimenti</a>| </li>
	      <li><a href="contacts.php">Contatti</a></li>
	    </ul>
	    <p class="copyright">©education zone. All right reserved.</p>
	    <ul class="templateworld">
	      <li>design by:</li>
	      <li><a href="http://www.templateworld.com" target="_blank">Template World</a></li>
	    </ul>
	    <div id="footerPanhtml"><a href="http://validator.w3.org/check?uri=referer" target="_blank">HTML</a></div>
	    <div id="footerPancss"><a href="http://jigsaw.w3.org/css-validator/check/referer" target="_blank">css</a></div>
	  </div>
	</div>
</body>
</html>
